<template>
    <vol-box
      :lazy="true"
      v-model="model"
      title="选择运维服务项目"
      :height="400"
      :width="1200"
      :padding="5"
      :onModelClose="onModelClose"
    >
      <div>
        <operations_projectCom ref="operations_projectCom"></operations_projectCom>
      </div>
      <template #footer>
        <div>
          <el-button type="primary" size="small" @click="onSelect"
            >确认</el-button
          >
        </div>
      </template>
    </vol-box>
  </template>
  <script>
  import VolBox from "@/components/basic/VolBox.vue";
  import operations_projectCom from "@/views/operationsinfo/operations_project/q_operations_project.vue";
  //这里使用的vue2语法，也可以写成vue3语法
  export default {
    components: {
      "vol-box": VolBox,
      operations_projectCom,
    },
    data() {
      return {
        model: false,
      };
    },
    methods: {
      async onModelClose() {
        await this.$refs.operations_projectCom.$refs.grid.search();
      },
      open() {
        this.model = true;
        // let setIntervalTiem = setInterval(() => {
        //   if (this.$refs.operations_projectCom) {
        //     clearInterval(setIntervalTiem);
        //     this.$refs.operations_projectCom.searchFormFields.AuditStatus = "1";
        //    if(this.$route.path == "/q_operations_return" ){
        //     this.$refs.operations_projectCom.searchFormFields.WHSE_no = WHSE_no;
        //     this.$refs.operations_projectCom.searchFormOptions.forEach(item => {
        //         item.forEach(x => {
        //           if(x.field == "WHSE_no"){
        //             x.disabled = true
        //           }
        //         })
        //      })
        //    }
        //     this.$refs.operations_projectCom.$refs.grid.search();
        //   }
        // }, 100);
      },
  
      onSelect() {
        let rowObj = this.$refs.operations_projectCom.$refs.grid.getSelectRows();
        if (!rowObj.length) {
            this.base.ElMessageFn("请至少选择一条数据");
          return (this.model = true);
        }
        let AuditStatusSome = rowObj.some((item) => item.AuditStatus != 1);
        if (AuditStatusSome) {
            this.base.ElMessageFn("请勿选择审核未通过单据");
          return;
        }
        this.$emit("parentCall", ($parent) => {
          try {
            //处理明细表已含有相同数据
            let parentList = $parent.$refs.detail.rowData;
            let set = new Set(
              parentList.map((item) => item.Operations_project)
            ); //得到由明细表goods_sub组成的数组
            let commonObjects = rowObj.filter((item) => set.has(item.Operations_project)); //得到明细表中已包含的选中行
            let goodsNamesSet = new Set(commonObjects.map((item) => item.Operations_project)); //相同商品的编码
            if (commonObjects.length) {
              rowObj = rowObj.filter((item) => !goodsNamesSet.has(item.Operations_project)); //在选中数据中剔除明细表已包含的行数据
              let str = "";
              commonObjects.forEach((r) => {
                if (str == "") str += r.Name;
                else str = str + "," + r.Name;
              });
              this.$message.warning(`${str}已存在此明细表`);
            }
            // console.log("$parent.editFormFields",$parent.editFormFields)
            let arr;
            if (this.$route.path == "/q_operations_site") {
              arr = rowObj.map((item) => {
                return {
                  Voucher_sub: $parent.editFormFields.Voucher_sub,
                  Operations_contract_sub:  $parent.editFormFields.Operations_contract_sub,
                  Operations_contract:  $parent.editFormFields.Operations_contract,
                  Client_name:  $parent.editFormFields.Client_name,
                  Site:  $parent.editFormFields.Site,
                  Operations_project: item.Operations_project,
                  Code: item.Code,
                  Name: item.Name,
                  Interval: item.Interval,
                  First_operations_time: this.base.getDate(false),
                };
              });
            }

            $parent.$refs.detail.rowData.unshift(...arr);
          } catch (error) {
            console.log(error);
          }
        });
        this.onModelClose();
        return (this.model = false);
      },
    },
  };
  </script>